package com.dmlll.dao.impl;

import com.dmlll.dao.UserDao;
import com.dmlll.demo.User;
import com.dmlll.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    public List<User> findByUserName(String username) {
        String sql = "select * from user where username = ?";
        List<User> list = new ArrayList<>();
        Connection connection = DButil.getConnection();
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                list.add(user);
            }
            DButil.close(connection,ps,rs);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
